package com.fengwk.support.spring.boot.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;

@ConfigurationProperties("web-socket")
@EnableWebSocketMessageBroker
public abstract class AbstractWebSocketMessageBrokerConfig extends AbstractWebSocketMessageBrokerConfigurer {
	
	public final String endpointWebSocket = "/ws";// webSocket连接端点
	public final String endpointSockJs = "/ws/sockjs";// sockjs连接端点
	public final String endpointAllowedOrigins = "*";// 端点跨域配置
	
	public final String brokerTopic = "/topic";
	public final String brokerUser = "/user";
	
	public final String destinationPrefixeApp = "/ws/";
//	public String destinationPrefixeUser = "/user/";

	@Override
	public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
		// 配置服务连接端点,客户端socket必须先连接至服务器服务端点
		stompEndpointRegistry.addEndpoint(endpointWebSocket).setAllowedOrigins(endpointAllowedOrigins);
		stompEndpointRegistry.addEndpoint(endpointSockJs).setAllowedOrigins(endpointAllowedOrigins).withSockJS();
	}

	@Override
	public void configureMessageBroker(MessageBrokerRegistry registry) {
		// 定义可以向客户端发消息的域
		registry.enableSimpleBroker(brokerTopic, brokerUser);
		// 客户端向服务器端发送主题的前缀
		registry.setApplicationDestinationPrefixes(destinationPrefixeApp);
		// 指定用户发送一对一的消息主题的前缀
//		registry.setUserDestinationPrefix(destinationPrefixeUser);
	}

	public String getEndpointWebSocket() {
		return endpointWebSocket;
	}

	public String getEndpointSockJs() {
		return endpointSockJs;
	}

	public String getEndpointAllowedOrigins() {
		return endpointAllowedOrigins;
	}

	public String getBrokerTopic() {
		return brokerTopic;
	}

	public String getBrokerUser() {
		return brokerUser;
	}

	public String getDestinationPrefixeApp() {
		return destinationPrefixeApp;
	}
	
}
